/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "header.h"
#include "menubar.h"
#include "mainwindow.h"
#include "mypopupmenu.h"
#include "config.h"
#include "fork.h"
#include "channel.h"

MenuBar::MenuBar (QWidget * parent, const char *name):
QMenuBar (parent, name),
mainwindow (static_cast < Sopcast * >(parent))
{

  //popupmenu
  QPopupMenu *file = new QPopupMenu (this);
//  file->insertItem ("Focus", editsearch, SLOT (setFocus ()), Key_Escape);
  file->insertItem ("Exit", qApp, SLOT (quit ()), CTRL + Key_Q);
  insertItem ("&File", file);

  QPopupMenu *config = new QPopupMenu (this);
  config->insertItem ("&Config", this, SLOT (onconfigclicked ()),
		      CTRL + Key_F);
  insertItem ("&Config", config);


  history = new QPopupMenu (this);
  connect (history, SIGNAL (activated (int)), this,
	   SLOT (onchoosehistory (int)));
  insertItem ("&History", history);


  bookmark = new MyPopupMenu (this);
  bookmark->insertItem ("Bookmark", this,
			SLOT (onbookmarkclicked ()), CTRL + Key_B);
  bookmark->insertSeparator ();
  connect (bookmark, SIGNAL (activated (int)), this,
	   SLOT (onchoosebookmark (int)));
  insertItem ("&Bookmark", bookmark);


  QPopupMenu *help = new QPopupMenu (this);
  help->insertItem ("&About", this, SLOT (about ()), CTRL + Key_H);
  insertItem ("&Help", help);


  setSeparator (QMenuBar::InWindowsStyle);

}

void
MenuBar::onconfigclicked ()
{
  vboxconfig->
    move (mapToGlobal
	  (QPoint ((mainwindow->width () - vboxconfig->width ()) / 2,
		   (mainwindow->height () - vboxconfig->height ()) / 2)));
  vboxconfig->show ();

}


void
MenuBar::onchoosehistory (int i)
{
  QString str = history->text (i);
  QString name =
    str.mid (0, str.find (vboxconfig->editchannelheader->text ()));
  QString url = str.mid (str.find (vboxconfig->editchannelheader->text ()));
  //launch sop
  fork->killsop ();
  fork->killplayer ();
  mainwindow->channelname = name;
  if (fork->forksop (url) == TRUE)
    {
      mainwindow->message[0]->setText ("Connecting...");
    }
  else
    fprintf (stderr, "failed to launch sp-sc!\n");

}


void
MenuBar::onchoosebookmark (int id)
{
//  printf ("choose index=%d,id=%d\n", bookmark->indexOf (id), id);
  if (id >= 2)
    {
      if (bookmark->rightbutton)	///delete
	{
	  bookmark->removeItem (id);
	}
      else			///launch
	{
	  QString str = bookmark->text (id);
	  QString name =
	    str.mid (0, str.find (vboxconfig->editchannelheader->text ()));
	  QString url =
	    str.mid (str.find (vboxconfig->editchannelheader->text ()));
	  //launch sop
	  fork->killsop ();
	  fork->killplayer ();
	  mainwindow->channelname = name;
	  if (fork->forksop (url) == TRUE)
	    {
	      mainwindow->message[0]->setText ("Connecting...");
	    }
	  else
	    fprintf (stderr, "failed to launch sp-sc!\n");
	}
    }
}

void
MenuBar::onbookmarkclicked ()
{
  QListViewItem *item = channel->currentItem ();
  if (item == 0)
    return;
  if (item->depth () == 1)
    {
      int index = 2;
      bool flag = FALSE;
      while (bookmark->idAt (index) != -1)
	{
//        printf ("index=%d,id=%d\n", index, bookmark->idAt (index));
	  if (bookmark->text (bookmark->idAt (index)) ==
	      (item->text (NAME_COLUMN) + " " + item->text (URL_COLUMN)))
	    flag = TRUE;
	  index++;
	}

      if (flag == FALSE)
	{
	  bookmark->insertItem (item->text (NAME_COLUMN) + " " +
				item->text (URL_COLUMN), idbookmark);
	  idbookmark++;
	}
    }
}


#include "mozart.xpm"
void
MenuBar::about ()
{
  const QPixmap p1 (mozart);
  QMessageBox m;
  m.setCaption ("qsopcast");
  m.setIconPixmap (p1);
  m.setText ("qsopcast: a QT front-end to p2p TV sopcast.\n"
	     "(c)2005-2006 Liu Di. All rights reserved.\n"
	     "Maintainer: Wei Lian\n"
	     "Developers: Liu Di, Wei Lian\n"
	     "http://lianwei3.googlepages.com/home2");
  m.exec ();
}
